//
//  jingxuanViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/19.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "jingxuanViewController.h"
#import "Masonry.h"
#import "jingxuanViewCell.h"
#import "GPosAPI.h"
#import "homeModel.h"
#import "SVProgressHUD.h"
#import "jxDetaillViewController.h"
#import "jxBullViewController.h"
#import "jxlsViewController.h"
#import "XMRegisterViewController.h"
#import "MJRefresh.h"


@interface jingxuanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)NSMutableArray * modelArr;
@property (nonatomic,strong)UIView * qstView;


@end

@implementation jingxuanViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    
    [self loadData];
    
}

- (void)loadData {
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{@"userId" : @(user)};
    [GPosAPI requestCampaignBillWithParams:params andCallback:^(id obj) {
        NSLog(@"--%@",obj);
        if ( obj == nil ){
            
            [SVProgressHUD showInfoWithStatus:@"请求失败"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else {
            
            self.modelArr = [[NSMutableArray alloc] init];
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                NSArray * arr = [[NSArray alloc] init];
                arr = [obj objectForKey:@"data"];
                for (NSDictionary * dic in arr) {
                    homeModel *model = [[homeModel alloc] init];
                    
                    
                    model.oneint = [[dic objectForKey:@"freezeDays"] integerValue];//判断颜色
                    model.twoint = [dic objectForKey:@"phase"];//第几期
                    model.threeint =[dic objectForKey:@"minerDividend"] ;//新增GPOS收益
                    model.fourint = [dic objectForKey:@"total_miner_dividend"];//累计收益
                    //                    model.fiveint = [dic objectForKey:@"freezeDays"];//带释放天数
                    model.startchuo = [dic objectForKey:@"startTime"];//开始时间戳
                    model.Aount = [dic objectForKey:@"prize"] ;//第几名
                    model.periods = [dic objectForKey:@"periods"];//周期
                    model.participant = [[dic objectForKey:@"participant"] integerValue];//参与人数
                    model.paiming = [[dic objectForKey:@"rank"] integerValue];//排名
                    model.amount = [dic objectForKey:@"amount"];//竟选数额
                    [self.modelArr addObject:model];
                }
                
                
                
                NSLog(@"self---%@",[obj objectForKey:@"data"]);
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else {
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }
        }
        
        
        [self tableViewLoadView];
    }];
    
    
}


- (void)tableViewLoadView{
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    UIView * footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    footerView.frame = CGRectMake(0, 0, KmainScreenWidth, 30);
    self.tableView.tableFooterView = footerView;
    if (iPhoneX) {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(10);
            make.left.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.bottom.equalTo(self.view).with.offset(-kBottomSafeHeight);
            
        }];
    }else {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(10);
            make.left.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.bottom.equalTo(self.view).with.offset(-10);
            
        }];
        
    }
    
    self.qstView = [[UIView alloc] init];
    self.qstView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.qstView];
    if (iPhoneX) {
        [self.qstView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(0);
            make.left.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.bottom.equalTo(self.view).with.offset(-kBottomSafeHeight);
            
        }];
    }else {
        [self.qstView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(0);
            make.left.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.bottom.equalTo(self.view).with.offset(-10);
            
        }];
        
    }
    self.qstView.hidden = YES;
    UIImageView * qstImage = [[UIImageView alloc] init];
    qstImage.image = [UIImage imageNamed:@"icon_zwmingxi"];
    [self.qstView addSubview:qstImage];
    [qstImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.qstView);//
        make.top.equalTo(self.qstView).with.offset(KmainScreenHeiht/4);
    }];
    UILabel * qstLabel = [[UILabel alloc] init];
    qstLabel.text = @"暂无明细";
    qstLabel.textColor = [UIColor grayColor];
    qstLabel.font = [UIFont systemFontOfSize:13.f];
    [self.qstView addSubview:qstLabel];
    [qstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qstImage.mas_bottom).with.offset(5);
        make.centerX.equalTo(qstImage);
    }];
    
    if (self.modelArr.count == 0) {
        self.tableView.hidden = YES;
        self.qstView.hidden = NO;
    }else {
        self.tableView.hidden = NO;
        self.qstView.hidden = YES;
        
    }

    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//rankingCell
    
    
    jingxuanViewCell * cell = [[jingxuanViewCell alloc] init];
    cell = [tableView dequeueReusableCellWithIdentifier:@"jingxuanCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"jingxuanViewCell" owner:self options:nil] firstObject];
    }
    
    
    if ([[self.modelArr[indexPath.row] Aount] integerValue] ==1 ) {
        cell.touxiangLabel.text = @"一等奖";
    }else if ([[self.modelArr[indexPath.row] Aount] integerValue] ==2){
        cell.touxiangLabel.text = @"二等奖";
        
    }else if ([[self.modelArr[indexPath.row] Aount] integerValue] ==3){
        cell.touxiangLabel.text = @"三等奖";
        
    }
    if ([self.modelArr[indexPath.row] oneint] > 0) {
        
        cell.touxiangView.backgroundColor = [UIColor blueColor];
        cell.touxiangView.layer.cornerRadius = 5;
//        NSNumber * nsNumber1 = @([[self.modelArr[indexPath.row] fourint] floatValue]);
        cell.dingbuLabel.text = [NSString stringWithFormat:@"累计收益:%@ GPOS",@([self.modelArr[indexPath.row] fourint].floatValue)];
//        NSNumber * nsNumber2 = @([[self.modelArr[indexPath.row] threeint] floatValue]);
        cell.zhongjianLabel.text = [NSString stringWithFormat:@"新增GPOS智能收益:%@ GPOS",@([self.modelArr[indexPath.row] threeint].floatValue)];
        
        cell.dibuLabel.text = [NSString stringWithFormat:@"待释放天数:%ld",(long)[self.modelArr[indexPath.row] oneint]];
        
        
    }else {
        
        cell.touxiangView.backgroundColor = [UIColor grayColor];
        cell.touxiangView.layer.cornerRadius = 5;
        if ([[self.modelArr[indexPath.row] Aount] intValue] == -2) {
            
            cell.zhongjianLabel.text = @"未达到开奖人数";
            cell.zhongjianLabel .textColor = [UIColor grayColor];
            cell.touxiangLabel.text = @"未开奖";
            cell.dingbuLabel.hidden = YES;
            cell.dibuLabel.hidden = YES;
        }else if ([[self.modelArr[indexPath.row] Aount] intValue] == -1) {
            
            cell.zhongjianLabel.text = @"未竞选上";
            cell.zhongjianLabel .textColor = [UIColor grayColor];
            ///           cell.touxiangLabel.text = @"未开奖";
            cell.touxiangImage.image = [UIImage imageNamed:@"icon_wxs"];
            cell.dingbuLabel.hidden = YES;
            cell.dibuLabel.hidden = YES;
            cell.touxiangLabel .hidden = YES;
            
        }else{
            
            cell.dingbuLabel.hidden = YES;
            cell.dibuLabel.hidden = YES;
//             NSNumber * nsNumber1 = @([[self.modelArr[indexPath.row] fourint] floatValue]);
            cell.zhongjianLabel.text = [NSString stringWithFormat:@"累计收益:%@ GPOS",@([self.modelArr[indexPath.row] fourint].floatValue)];
            
            
        }
        
    }
    cell.bjkView.layer.cornerRadius = 5;
    long endtime = [[self.modelArr[indexPath.row] periods] longLongValue] *24*60*60*1000+[[self.modelArr[indexPath.row] startchuo] longLongValue];
    
    NSLog(@"%lu",endtime);
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = [self.modelArr[indexPath.row] startchuo];
    NSString * endStr = [NSString stringWithFormat:@"%lu",endtime];
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval=[timeStampString doubleValue] / 1000.0;
    NSTimeInterval endtimeter = [endStr doubleValue] /1000.0;
    NSDate *date  = [NSDate dateWithTimeIntervalSince1970:interval];
    
    
    NSDate * endDate = [NSDate dateWithTimeIntervalSince1970:endtimeter];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString    = [formatter stringFromDate: date];
    NSString * enddatesteinbg = [formatter  stringFromDate:endDate];
    
    cell.topTimeLabel.text = [NSString stringWithFormat:@"第%@期 : %@--%@",[self.modelArr[indexPath.row] twoint],dateString,enddatesteinbg];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    homeModel * model = self.modelArr[indexPath.row];
    if ([[self.modelArr[indexPath.row] Aount] integerValue] == -1) {
        jxDetaillViewController * VC = [[jxDetaillViewController alloc] init];
        
        
        VC.fourint =model.fourint;
        VC.twoint = model.twoint;
        VC.threeint = model.threeint;
        VC.startchuo = model.startchuo;
        VC.Aount = model.Aount;
        VC.periods = model.periods;
        VC.participant = model.participant;
        VC.paiming = model.paiming;
        VC.amount = model.amount;
        
        
        [self.navigationController pushViewController:VC animated:YES];
        NSLog(@"为选上");
        
    }else if ([[self.modelArr[indexPath.row] Aount] integerValue] == 1 || [[self.modelArr[indexPath.row] Aount] integerValue] == 2 ||[[self.modelArr[indexPath.row] Aount] integerValue] == 3) {
        
        if ([self.modelArr[indexPath.row] oneint] > 0) {
            NSLog(@"竞选实况");
            jxBullViewController * VC = [[jxBullViewController alloc] init];
            VC.fourint =model.fourint;
            VC.twoint = model.twoint;
            VC.threeint = model.threeint;
            VC.startchuo = model.startchuo;
            VC.Aount = model.Aount;
            VC.periods = model.periods;
            VC.participant = model.participant;
            VC.paiming = model.paiming;
            VC.amount = model.amount;
            VC.oneint = model.oneint;
            
            [self.navigationController pushViewController:VC animated:YES];
            
        }else {
            jxlsViewController *VC = [[jxlsViewController alloc] init];
            VC.fourint =model.fourint;
            VC.twoint = model.twoint;
            VC.threeint = model.threeint;
            VC.startchuo = model.startchuo;
            VC.Aount = model.Aount;
            VC.periods = model.periods;
            VC.participant = model.participant;
            VC.paiming = model.paiming;
            VC.amount = model.amount;
            VC.oneint = model.oneint;
            [self.navigationController pushViewController:VC animated:YES];
            NSLog(@"竞选历史");
        }
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
